<?php
require_once ("class_managed_form.php");
require_once ('class_layout.php');

/*
  TODO: Implement top right buttons. Maybe make one class with that in it and
  both this and the FormPage classes could inherit?
*/

class ManagedFormPage extends ManagedForm
{
  public $layout;

  const FEEDBACK_ADD = 'Record added successfully';
  const FEEDBACK_COPY = 'Record copied successfully';
  const FEEDBACK_EDIT = 'Record edited successfully';
  const FEEDBACK_DELETE = 'Record(s) deleted successfully';

  public function __construct($title, $mysqlTable, $mysqlPrimaryKey,
      $mysqlDatabase = null) {
    $this->layout = new Layout($title);
    $this->layout->attachJavascript(HTTP_COMMON . 'javascript-form.js');
    $this->layout->attachJavascript(HTTP_COMMON . 'javascript-datechooser.js');
    $this->layout->attachJavascript(HTTP_COMMON . 'javascript-autocomplete.js');
    $this->layout->attachJavascript(HTTP_COMMON . 'javascript-animation.js');
    $this->layout->attachJavascript(HTTP_COMMON . 'tiny_mce/tiny_mce.js');
    $this->layout->attachStylesheet(HTTP_COMMON . 'style-form.css');
    $this->layout->attachStylesheet(HTTP_COMMON . 'style-datechooser.css');
    $this->layout->attachStylesheet(HTTP_COMMON . 'style-autocomplete.css');

    parent::__construct($mysqlTable, $mysqlPrimaryKey, $mysqlDatabase);
  }

  protected function redirect()
  {
    switch ($this->mode) {
    case self::MODE_ADD:
      Util::setSessionMessage(self::FEEDBACK_ADD);
      break;
    case self::MODE_COPY:
      Util::setSessionMessage(self::FEEDBACK_COPY);
      break;
    case self::MODE_EDIT:
      Util::setSessionMessage(self::FEEDBACK_EDIT);
      break;
    case self::MODE_DELETE:
      Util::setSessionMessage(self::FEEDBACK_DELETE);
      break;
    }
    parent::redirect();
  }

  public function show() {
    switch ($this->mode) {
      case self::MODE_ADD:
        $this->layout->title = 'Add ' . $this->layout->title;
        break;
      case self::MODE_EDIT:
        $this->layout->title = 'Edit ' . $this->layout->title;
        break;
      case self::MODE_COPY:
        $this->layout->title = 'Copy ' . $this->layout->title;
        break;
      case self::MODE_DELETE:
        $this->layout->title = 'Delete ' . $this->layout->title;
        break;
    }
    $this->process();
    $this->layout->showHeader();
    $this->display();
    $this->layout->showFooter();
  }
}
?>